package com.rjh.base.socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketSever {

    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("开始接收消息。。。");
        Socket socket = serverSocket.accept();

        //定于本地用于读取文件的输出流
        FileOutputStream fos = new FileOutputStream("E:\\tmp\\test_copy.png");

        //socket获取对话的输出流
        InputStream inputStream = socket.getInputStream();

        byte[] bytes = new byte[1024];
        int readLen;
        while ((readLen=inputStream.read(bytes))!=-1){
            fos.write(bytes,0,readLen);
        }


        System.out.println("接收图片成功,已成功保存");

        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("接收图片成功,已成功保存".getBytes());
        socket.shutdownOutput();

        //关闭资源
        outputStream.close();
        inputStream.close();
        fos.close();
        socket.close();

    }
}
